package leeon.bbsbrower.views;

import ipworks.IPWorksException;
import leeon.bbsbrower.actions.BBSMsgAction;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class MsgDialog extends Dialog {
	
	
	private Text inText = null;
	private Text outText = null;
	private Text allText = null;
	
	private String destid = null;
	private String id = null;
	private String msg = null;
	
	/**
	 * Create the dialog
	 */
	public MsgDialog(Shell parentShell, String msg, String id, String destid) {
		super(parentShell);
		this.msg = msg;
		this.id = id;
		this.destid = destid;
	}
	
	/**
	 * Create contents of the dialog
	 * @param parent
	 */
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(null);
				
		inText = new Text(container, SWT.BORDER|SWT.READ_ONLY);		
		inText.setBounds(120, 10, 740, 30);
		inText.setText(this.msg);
		inText.setFont(ApplicationWorkbenchWindowAdvisor.listFont);
		
		inText.addKeyListener(new KeyListener() {

			public void keyPressed(KeyEvent e) {
				//回车忽略
				if (e.keyCode == 13) {
					MsgDialog.this.close();
					e.doit = false;
				
				//回复	
				} else if (e.character == 'r') {
					MsgDialog.this.getShell().setSize(880, 120);
					outText.setVisible(true);
					outText.setFocus();
					e.doit = false;
				}
			}

			public void keyReleased(KeyEvent e) {}
			
		});
		
		final Label inLabel = new Label(container, SWT.NONE);
		inLabel.setText(this.id);
		inLabel.setBounds(10, 10, 100, 30);
		inLabel.setFont(ApplicationWorkbenchWindowAdvisor.listFont);
		
		outText = new Text(container, SWT.BORDER);
		outText.setBounds(10, 50, 850, 30);
		outText.setFont(ApplicationWorkbenchWindowAdvisor.listFont);
		outText.setVisible(false);
		outText.addKeyListener(new KeyListener() {

			public void keyPressed(KeyEvent e) {
				//回车发送
				if (e.keyCode == 13) {
					actionReMsg();					
					e.doit = false;
				
				//Ctrl + z查看以前的短信	
				} else if (e.stateMask == SWT.CTRL && e.keyCode == 122) {					
					actionAllMsg();					
					e.doit = false;
				}
			}

			public void keyReleased(KeyEvent e) {}
			
		});


		allText = new Text(container, SWT.BORDER|SWT.READ_ONLY|SWT.V_SCROLL);		
		allText.setBounds(10, 90, 850, 400);	
		allText.setFont(ApplicationWorkbenchWindowAdvisor.listFont);
		allText.setVisible(false);
		
		return container;
	}
	
	
    /**
     * 设定窗口的标题和图片
     */
    protected void configureShell(Shell shell) {
    	super.configureShell(shell);
    	shell.setText("新消息");    	
    }
	
	/**
	 * 发送
	 *
	 */
	private void actionReMsg() {
		if (outText.getText() == null || outText.getText().length() == 0) {
			this.close();
		}
		
		try {
			BBSMsgAction.sendMsg(this.id, outText.getText(), this.destid);
			this.close();
		} catch (Exception e) {
			MessageDialog.openWarning(this.getShell(), "错误信息", e.toString());
			outText.setFocus();
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 查看以前的短信
	 *
	 */
	private void actionAllMsg() {		
		if (!allText.getVisible()) {
			try {
				allText.setText(BBSMsgAction.getAllMsg());
				this.getShell().setSize(880, 530);
				this.getShell().setLocation(this.getShell().getLocation().x, 150);
				allText.setVisible(true);
				
			} catch (IPWorksException e) {
				MessageDialog.openError(this.getShell(), "错误信息", e.toString());
				e.printStackTrace();
			}
						
		}		
	}
	
	/**
	 * 去掉下面的button bar
	 */
	protected Control createButtonBar(Composite parent) {
		return null;
	}
	
	/**
	 * Return the initial size of the dialog
	 */
	protected Point getInitialSize() {
		return new Point(880, 80);
	}

}
